/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.api.beActionAssemblerGen;

import com.inspur.edp.bef.api.attr.AssemblerMethodAttribute;
import com.inspur.edp.bef.bizentity.operation.BizAction;
import com.inspur.edp.bef.bizentity.operation.internalbeaction.DeleteBEAction;
import com.inspur.edp.bef.bizentity.operation.internalbeaction.ModifyBEAction;
import com.inspur.edp.bef.bizentity.operation.internalbeaction.RetrieveBEAction;
import com.inspur.edp.bef.spi.action.assembler.entityAssemblerFactory.IDefaultEntityActionAssFactory;
import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.AnnotationType;
import com.inspur.edp.caf.generator.baseInfo.InterfaceMethodGenerateInfo;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.InterfaceMethodGenerator;
import com.inspur.edp.caf.generator.method.InterfaceMethodGeneratorContext;
import com.inspur.edp.cef.generator.overall.Utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.StringLiteral;

public class InternalBEActionMethodGenerator extends InterfaceMethodGenerator {

  // region Basic Info
  private BizAction action;

  public InternalBEActionMethodGenerator(BizAction action) {
    this.action = action;
  }

  // region Static Members
  private static String getAssemblerMethodName(BizAction action) {
    AssemblerMethodAttribute attr =
        getMethodInfo(action).getAnnotation(AssemblerMethodAttribute.class);
    return attr.value();
  }

  public static String getMethodName(BizAction action) {
    if (action instanceof RetrieveBEAction) {
      return "getRetrieveActionAssembler";
    }
    if (action instanceof ModifyBEAction) {
      return "getModifyActionAssembler";
    }
    if (action instanceof DeleteBEAction) {
      return "getDeleteActionAssembler";
    }
    throw new IllegalArgumentException();
  }

  public static java.util.ArrayList<ParameterInfo> getMethodParameters(BizAction action) {
    return new ArrayList<ParameterInfo>(
        Arrays.stream(getMethodInfo(action).getParameters())
            .map(
                item -> {
                  ParameterInfo tempVar = new ParameterInfo();
                  tempVar.setParamName(item.getName());
                  tempVar.setParamType(
                      com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(item.getType()));
                  return tempVar;
                })
            .collect(Collectors.toList()));
  }

  public static TypeInfo getReturnType(BizAction action) {
    return new TypeInfo(getMethodInfo(action).getReturnType().getTypeName());
  }

  private static java.lang.reflect.Method getMethodInfo(BizAction action) {
    Method methodInfo = null;
//    try {
      Method[] methods = IDefaultEntityActionAssFactory.class.getMethods();
      for(Method method : methods)
      {
        if(method.getName().equals( getMethodName(action)))
        {methodInfo =method;
        break;}
      }
//      methodInfo = IDefaultEntityActionAssFactory.class.getMethod(getMethodName(action));
//    } catch (NoSuchMethodException e) {
//      throw new RuntimeException(e);
//    }
    if (methodInfo == null) {
      throw new RuntimeException(action.getCode() + "不是内置动作");
    }
    return methodInfo;
  }

  @Override
  protected InterfaceMethodGeneratorContext createClassInfoContext() {
    return new InterfaceMethodGeneratorContext();
  }

  @Override
  protected InterfaceMethodGenerateInfo createClassMethodInfo() {
    return new InterfaceMethodGenerateInfo();
  }

  // endregion

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<ModifierKeyword>();
  }

  @Override
  protected String getMethodName() {
    return getMethodName(action);
  }

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    return getMethodParameters(action);
  }

  @Override
  protected TypeInfo getReturnType() {
    return getReturnType(action);
  }

  @Override
  protected java.util.ArrayList<AnnotationInfo> getAttributeList() {
    java.util.ArrayList<AnnotationInfo> tempVar = new java.util.ArrayList<AnnotationInfo>(1);
    AnnotationInfo info = new AnnotationInfo();
    info.setTypeInfo(
        com.inspur.edp.cef.generator.overall.Utils.createTypeInfo(AssemblerMethodAttribute.class));
    info.setAnnotationType(AnnotationType.SingleMember);
    StringLiteral valueLiteral = ast.newStringLiteral();
    valueLiteral.setLiteralValue(getAssemblerMethodName(action));
    info.setSingleParam(valueLiteral);
    tempVar.add(info);
    return tempVar;
  }

  // endregion
}
